#!/bin/bash

# Function to execute operations inside a container
execute_operations_in_container() {
    local container_name="custom-dind-$1"
    local network_id=$1

    echo "Executing prepare-all.sh in $container_name"
    docker exec "$container_name" /bin/bash -c "./prepare-all-offnet.sh"
    if [ $? -ne 0 ]; then
        echo "Failed to execute prepare-all-offnet.sh in $container_name"
    else
        echo "prepare-all.sh executed successfully in $container_name"
    fi

    echo "Running backend in $container_name"
    docker exec -d "$container_name" /bin/bash -c "cd /backend/cmd && go run main.go >> /logs/backend-$container_name.log 2>&1"
    if [ $? -ne 0 ]; then
        echo "Failed to run backend in $container_name"
    else
        echo "Backend running successfully in $container_name"
    fi

    ./refund-balances.sh "$network_id"

    if [[ "$container_name" == "custom-dind-3" || "$container_name" == "custom-dind-4" ]]; then
        echo "Changing contract in $container_name"
        docker exec "$container_name" /bin/bash -c "sed -i '11s/basic-token/evidence_chaincode/' /fabric-zkrollup/conf/common.go"
        if [ $? -ne 0 ]; then
            echo "Failed to change contract in $container_name"
            return
        fi
        echo "Contract changed successfully in $container_name"

        echo "Running make run TARGET=storage in $container_name"
        docker exec -d "$container_name" /bin/bash -c "cd /fabric-zkrollup/ && make run TARGET=storage >> /logs/storage-$container_name.log 2>&1"
        if [ $? -ne 0 ]; then
            echo "Failed to run make run TARGET=storage in $container_name"
        else
            echo "make run TARGET=storage executed successfully in $container_name"
        fi
    else
        echo "Running make run TARGET=transfer in $container_name"
        docker exec -d "$container_name" /bin/bash -c "cd /fabric-zkrollup/ && make run TARGET=transfer >> /logs/transfer-$container_name.log 2>&1"
        if [ $? -ne 0 ]; then
            echo "Failed to run make run TARGET=transfer in $container_name"
        else
            echo "make run TARGET=transfer executed successfully in $container_name"
        fi
    fi
}

# Main script logic
if [ -f "custom-dind.tar" ]; then
    echo "Image custom-dind.tar found, loading..."
    docker load --input custom-dind.tar
else
    echo "Image custom-dind.tar not found, building..."
    if [ ! -f "Dockerfile" ]; then
        echo "Dockerfile not found!"
        exit 1
    fi
    docker build -t custom-dind:latest .
    if [ $? -ne 0 ]; then
        echo "Failed to build custom-dind image. Exiting..."
        exit 1
    fi
    echo "Image custom-dind:latest built successfully."
    docker save -o custom-dind.tar custom-dind:latest
    if [ $? -ne 0 ]; then
        echo "Failed to save custom-dind image as custom-dind.tar. Exiting..."
        exit 1
    fi
    echo "Image custom-dind:latest saved as custom-dind.tar successfully."
fi

for i in {1..1}; do
    echo "Starting container custom-dind-$i"
    ./start_container.sh "$i" 

    echo "Executing operations in container custom-dind-$i"
    execute_operations_in_container "$i" 
done

# Wait for all operations to complete
wait
echo "All operations completed."
